package com.yycx.module.bus.provider.strategy.impl;

import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.groovy.provider.script.MyGroovyScriptEngine;
import com.yycx.module.bus.client.entity.hy.BusPolicyPosition;
import com.yycx.module.bus.client.entity.hy.BusPolicySaleman;
import com.yycx.module.bus.client.entity.BusSaleman;
import com.yycx.module.bus.client.entity.hy.HyOrder;
import com.yycx.module.bus.client.entity.hy.HyOrderDetail;
import com.yycx.module.bus.provider.service.hy.BusPolicyPositionService;
import com.yycx.module.bus.provider.service.hy.BusPolicySalemanService;
import com.yycx.module.bus.provider.service.BusSalemanService;
import com.yycx.module.bus.provider.strategy.SettleStrategy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * 商务经理结算规则
 *
 * @author zyf
 */
@Service("settleStrategyD")
public class SettleStrategyD implements SettleStrategy {

    @Resource
    private BusSalemanService saleManService;

    @Resource
    private BusPolicyPositionService policyPositionService;

    @Resource
    private BusPolicySalemanService policySalemanService;

    @Resource
    private MyGroovyScriptEngine groovyScriptEngine;

    @Override
    public void setSettlePolicy(HyOrderDetail orderDetail, HyOrder hyOrder, Long saleManId) {
        //自定义结算政策
        Integer customPolicy = FlymeUtils.getInteger(orderDetail.getCustomPolicy(), 0);
        //查询结算对象
        BusSaleman saleManD = saleManService.getById(saleManId);
        //职务ID
        Long positionId = saleManD.getPositionId();
        //发货日期
        Date sendDate = orderDetail.getSendDate();
        //结算政策ID
        Long policyDrugId = orderDetail.getPolicyDrugId();
        BigDecimal settlePrice = orderDetail.getSettlePriceD();
        //结算标准
        BigDecimal settleRate = orderDetail.getSettleRateD();
        //最高加价
        BigDecimal makeUpPriceD = FlymeUtils.getBigDecimal(orderDetail.getMakeUpPriceD(), "0");
        String settleFormulaContent = FlymeUtils.getString(orderDetail.getSettleFormulaContentD(), "(销售单价-商务经理结算价)*数量*(1-商务经理标准)-渠道商结算金额;");
        //部门利润公式
        String deptProfitAmountFormula = "(商务经理结算价-部门结算价)*数量*(1-部门标准);";
        //税率差公式
        String taxRateDiffFormula = "(销售单价-商务经理结算价)*数量*((1-部门标准)-(1-商务经理标准));";
        String deptProfitAmountFormula1 = "";
        //结算规则(回款后结算)
        Integer settleRule = 1;
        Integer baopiao = 1;
        Integer hasTaxRateDiff = FlymeUtils.getInteger(hyOrder.getHasTaxRateDiff(), 1);
        if (customPolicy.equals(0)) {
            //获取职务结算政策
            BusPolicyPosition busPolicyPosition = policyPositionService.findBusPolicyPosition(policyDrugId, positionId, sendDate);
            if (FlymeUtils.isNotEmpty(busPolicyPosition)) {
                settlePrice = FlymeUtils.getBigDecimal(busPolicyPosition.getPositionPrice(), settlePrice);
                settleRate = FlymeUtils.getBigDecimal(busPolicyPosition.getPositionRate(), settleRate);
                makeUpPriceD = FlymeUtils.getBigDecimal(busPolicyPosition.getMakeUpPrice(), makeUpPriceD);
                settleFormulaContent = FlymeUtils.getString(busPolicyPosition.getPositionSettleFormula(), settleFormulaContent);
            }
            //获取业务员结算政策
            BusPolicySaleman policySaleMan = policySalemanService.findByPolicyDrugIdAndSaleManId(policyDrugId, saleManId, sendDate);
            if (FlymeUtils.isNotEmpty(policySaleMan)) {
                settlePrice = FlymeUtils.getBigDecimal(policySaleMan.getSaleManPrice(), settlePrice);
                settleRate = FlymeUtils.getBigDecimal(policySaleMan.getSaleManRate(), settleRate);
                settleFormulaContent = FlymeUtils.getString(policySaleMan.getSaleManSettleFormula(), settleFormulaContent);
                //价格取值方式
                Integer priceType = FlymeUtils.getInteger(policySaleMan.getPriceType(), 1);
                if (priceType.equals(2)) {
                    //使用成本价
                    settlePrice = FlymeUtils.getBigDecimal(orderDetail.getCostPrice(), settlePrice);
                }
                //结算规则(回款后结算)
                settleRule = FlymeUtils.getInteger(policySaleMan.getSettleRule(), 1);
                //是否包票(默认包票)
                baopiao = FlymeUtils.getInteger(policySaleMan.getBaopiao(), 1);
                deptProfitAmountFormula1 = policySaleMan.getDeptProfitAmountFormula();
                //最高加价
                makeUpPriceD = FlymeUtils.getBigDecimal(policySaleMan.getMakeUpPrice(), makeUpPriceD);

            }
        }
        orderDetail.setBaopiao(baopiao);
        orderDetail.setSettleRule(settleRule);
        orderDetail.setSettlePriceD(settlePrice);
        orderDetail.setSettleRateD(settleRate);
        orderDetail.setMakeUpPriceD(makeUpPriceD);
        //自定义结算政策,取自定义公式
        if (customPolicy.equals(1)) {
            settleFormulaContent = orderDetail.getSettleFormulaContentD();
            taxRateDiffFormula = orderDetail.getTaxRateDiffFormula();
            deptProfitAmountFormula = orderDetail.getDeptProfitAmountFormula();
        }
        //如果是加价模式，需要预留商务总负责人费用
        if (FlymeUtils.gtzero(makeUpPriceD)) {
            orderDetail.setSaleManIdE(1L);
            hyOrder.setSaleManIdE(1L);
            orderDetail.setSettleFormulaContentE("(销售单价-商务经理结算价)*数量*(1-商务经理标准)-(销售单价-渠道商结算价)*数量*(1-渠道商标准)-最高加价*数量*(1-商务经理标准);");
        }
        orderDetail.setSettleFormulaContentD(settleFormulaContent);
        orderDetail.setSaleManIdD(saleManD.getSaleManId());
        //部门利润公式
        orderDetail.setDeptProfitAmountFormula(deptProfitAmountFormula);
        if (hasTaxRateDiff.equals(1)) {
            //税率差公式
            orderDetail.setTaxRateDiffFormula(taxRateDiffFormula);
        } else {
            orderDetail.setTaxRateDiffFormula(null);
        }
        if (FlymeUtils.isNotEmpty(deptProfitAmountFormula1)) {
            orderDetail.setDeptProfitAmountFormula(deptProfitAmountFormula1);
        }
    }

    @Override
    public void setSettleAmount(HyOrderDetail hyOrderDetail, HyOrder hyOrder, Map<String, Object> variable) {
        //商务经理结算价
        BigDecimal settlePriceD = FlymeUtils.getBigDecimal(hyOrderDetail.getSettlePriceD(), "0");
        if (FlymeUtils.gtzero(settlePriceD)) {
            //销售数量
            BigDecimal saleNum = hyOrderDetail.getSaleNum();
            //销售价
            BigDecimal salePrice = hyOrderDetail.getSalePrice();
            //商务经理结算标准
            BigDecimal settleRateD = FlymeUtils.getBigDecimal(hyOrderDetail.getSettleRateD(), "0");
            //商务经理结算金额
            BigDecimal settleAmountD = (salePrice.subtract(settlePriceD)).multiply(saleNum).multiply(settleRateD);
            //商务经理结算公式
            String settleFormulaD = hyOrderDetail.getSettleFormulaContentD();
            //使用公式重新计算商务经理结算公式
            if (FlymeUtils.isNotEmpty(settleFormulaD)) {
                settleAmountD = groovyScriptEngine.executeBigDecimal(settleFormulaD, variable,2);
            }
            variable.put("商务经理结算金额", settleAmountD);
            hyOrderDetail.setSettleAmountD(settleAmountD);
        }
        //最高加价
        BigDecimal makeUpPriceD = FlymeUtils.getBigDecimal(hyOrderDetail.getMakeUpPriceD(), "0");
        if (FlymeUtils.gtzero(makeUpPriceD)) {
            //商务总负责人结算公式(加价预留)
            String settleFormulaE = hyOrderDetail.getSettleFormulaContentE();
            if (FlymeUtils.isNotEmpty(settleFormulaE)) {
                BigDecimal settleAmountE = new BigDecimal("0");
                //商务经理加价
                String addPriceFormula = "销售单价-商务经理结算价;";
                BigDecimal addPrice = groovyScriptEngine.executeBigDecimal(addPriceFormula, variable,2);
                //如果加价没有超过最高加价不计算商务总负责人预览
                if (FlymeUtils.isNotEmpty(addPrice) && FlymeUtils.gt(addPrice, makeUpPriceD)) {
                    settleAmountE = groovyScriptEngine.executeBigDecimal(settleFormulaE, variable,2);
                    if (FlymeUtils.gtzero(settleAmountE)) {
                        hyOrderDetail.setSettleAmountE(settleAmountE);
                    } else {
                        hyOrderDetail.setSettleAmountE(new BigDecimal("0"));
                        hyOrder.setSaleManIdE(null);
                        hyOrderDetail.setSaleManIdE(null);
                    }
                } else {
                    hyOrder.setSaleManIdE(null);
                    hyOrderDetail.setSaleManIdE(null);
                }
                variable.put("商务总负责人结算金额", settleAmountE);
            }
        }

    }
}
